layui.use(['jquery','layer','table'],function () {
    var $=layui.jquery;
    var layer = layui.layer;
    var table=layui.table;
    //获取用户信息
    var user = JSON.parse(localStorage.getItem('user'));
    var createId = user.data.userId;
    //按钮样式
    $('.btn_style').on('click',function () {
        $(this).addClass('btn_onclick').siblings().removeClass('btn_onclick');
        $(this).removeClass('btn_remove').siblings().addClass('btn_remove');
    })
    //页面表格
    getUserTable();
    //次数展示
    useCountTable('1','');
    //按钮点击
    showTable();
    function showTable(){
        var flag = true;//flag为true，调用前端用户表格，为false调用管理人员表格
        var buttons = 0;//buttons=0为全部，1为正常,2为已拦截，3为已删除
        //点击前端用户
        $('#users').on('click',function () {
            flag = true;
            if(buttons == 0){
                getUserTable();
                useCountTable('1','');
            }else if(buttons == 1){
                getUserTable('1');
                useCountTable('1','1');
            }else if(buttons == 2){
                getUserTable('2');
                useCountTable('1','2');
            }else if(buttons == 3){
                getUserTable('3');
                useCountTable('1','3');
            }
        });
        //点击管理人员
        $('#manager').on('click',function () {
            flag = false;
            if(buttons == 0){
                getTable();
                useCountTable('2','');
            }else if(buttons == 1){
                getTable('1');
                useCountTable('2','1');
            }else if(buttons == 2){
                getTable('2');
                useCountTable('2','2');
            }else if(buttons == 3){
                getTable('3');
                useCountTable('2','3');
            }
        });
        //点击全部评论
        $('#allComment').on('click',function () {
            buttons = 0;
            if(flag){
                getUserTable('');
                useCountTable('1','');
            }else {
                getTable('');
                useCountTable('2','');
            }
        })
        //点击正常评论
        $("#normalComment").on('click',function () {
            buttons = 1;
            if(flag){
                getUserTable('1');
                useCountTable('1','1');
            }else {
                getTable('1');
                useCountTable('2','1');
            }
        })
        //点击已拦截评论
        $("#interceptComment").on('click',function () {
            buttons = 2;
            if(flag){
                getUserTable('2');
                useCountTable('1','2');
            }else {
                getTable('2');
                useCountTable('2','2');
            }
        })
        //点击已删除评论
        $("#deleteComment").on('click',function () {
            buttons = 3;
            if(flag){
                getUserTable('3');
                useCountTable('1','3');
            }else {
                getTable('3');
                useCountTable('2','3');
            }
        })
    }
    //调用次数表格
    function useCountTable(userType,status) {
        $.ajax({
            url: '/comment/personalProductNum',
            type: 'post',
            dataType: 'json',
            data: {'createId':createId,'type':'3','userType':userType,'status': status},
            success: function (dat) {
                countTable(dat.data);
            }
        })
    }
    //次数表格
    function countTable(data) {
        table.render({
            elem: '#countTable',
            data: data,
            width: 1650,
            cols: [[
                {field: 'historyNum',title: '历史评论次数',align: 'center',width:411},
                {field: 'historyPeople',title: '历史评论人数',align: 'center',width:411},
                {field: 'todayNum',title: '今日评论次数',align: 'center',width:411},
                {field: 'todayPeople',title: '今日评论人数',align: 'center',width:411}
            ]],
            done: function (res) {
                $('#allCommentNum').text(res.data[0].allNum);
                $('#normalCommentNum').text(res.data[0].normalNum);
                $('#interceptCommentNum').text(res.data[0].interceptNum);
                $('#deleteCommentNum').text(res.data[0].deleteNum);
            }
        })
    }
    //获取前台用户评论数据
    function getUserTable(status){
        $.ajax({
            url: '/comment/getProductUserTable',
            type: 'post',
            dataType: 'json',
            data: {'createId':createId,'status':status},
            success: function (data) {
                dataTable(data.data);
            }
        })
    }
    //获取管理人员评论数据
    function getTable(status){
        $.ajax({
            url: '/comment/getProductTable',
            type: 'post',
            dataType: 'json',
            data: {'createId':createId,'status':status},
            success: function (data) {
                dataTable(data.data);
            }
        })
    }
    //前端用户表格渲染
    function dataTable(data) {
        table.render({
            elem: '#dataTable',
            title: '数据列表',
            data: data,
            toolbar: '#table_head',
            limit: 10,
            width: 1650,
            page:{
                layout:['prev','page','next','skip'],
                limits:[3,6,9],
                first:"首页",
                last:"尾页",
            },
            cols: [[
                {type:'checkbox',float:'left'},
                {title:'序号',align:'center',width:100,templet:function (data) {
                        return data.LAY_INDEX;
                    }},
                {field:'account',title:'成员账号',align:'center',width:210},
                {field:'showName',title:'昵称',align:'center',width:215},
                {field:'belongName',title:'所属商品',align:'center',width:215},
                {field:'commentDate',title:'发言时间',align:'center',width:215},
                {field:'content',title:'发言内容',align:'center',width:216},
                {field:'status',title:'状态',align:'center',width:210,templet:function (data) {
                        if(data.status == 1){
                            return "正常";
                        }else if(data.status == 2){
                            return '已拦截';
                        }else if(data.status == 3){
                            return '已删除';
                        }else {
                            return '暂无状态';
                        }
                    }},
                {field:'',title:'操作',align:'center',width:210,templet:function (data) {
                        if(data.status == 1){
                            return '<a onclick="javascript:checkTable('+ data.id +');">查看</a>&nbsp;&nbsp;<a onclick="javascript:interceptTable('+ data.id +","+"'"+data.content+"'" +');">拦截</a>&nbsp;&nbsp;<a onclick="javascript:deleteTable('+ data.id +');">删除</a>'
                        }else if(data.status == 2){
                            return '<a onclick="javascript:checkTable('+ data.id +');">查看</a>&nbsp;&nbsp;<a onclick="javascript:recoverTable('+ data.id +"," +"'"+data.content+"'" +');">恢复</a>&nbsp;&nbsp;<a onclick="javascript:deleteTable('+ data.id +');">删除</a>'
                        }else if(data.status == 3){
                            return '<a onclick="javascript:checkTable('+ data.id +');">查看</a>';
                        }else{
                            return '无操作';
                        }
                    }},
            ]]
        })
    }
    //查看评论详情
    checkTable = function checkTable(id){
        $.ajax({
            url: '/comment/getProductInformation',
            type: 'post',
            dataType: 'json',
            data: {'id':id},
            success: function (data) {
                localStorage.setItem('product',JSON.stringify(data.data));
                layer.open({
                    type:2,
                    title:"商品详情",
                    area:['95%','100%'],
                    Boolean:true,
                    maxmin:true,
                    closeBtn:1,
                    btn:["确定","取消"],
                    offset:['0px','35px'],
                    content:"/comment/productreview/product-details.html",
                    success: function (layero,index) {
                        layero.find('.layui-layer-max').remove();
                        layero.find('.layui-layer-min').remove();
                        var child = layero.find('iframe').contents();
                        child.find('#parentId').text(id);
                    }
                })
            }
        })
    }
    //拦截评论
    interceptTable = function interceptTable(id, content) {
        layer.confirm('您确定要拦截'+ "‘" + content +"’" + "信息吗？",
            function (index) {
                updateStatus('2',id);
            })
    }
    //恢复评论
    recoverTable = function recoverTable(id, content) {
        layer.confirm('您确定要恢复'+ "‘" + content +"’" + "信息吗？",
            function (index) {
                updateStatus('1',id);
            })
    }
    //修改评论状态
    function updateStatus(status,id) {
        $.ajax({
            url: '/comment/updateStatus',
            type: 'post',
            dataType: 'json',
            data: {'status':status,'id':id},
            success: function (data) {
                if(data.data){
                    location.reload();
                }else {
                    layer.msg('操作失败')
                }
            }
        })
    }
    //删除
    deleteTable = function deleteTable(id){
        layer.confirm('您确定要删除此条评论吗？',
            function (index) {
                deleteComment(id);
            })
    }
    function deleteComment(id){
        $.ajax({
            url: '/comment/deleteComment',
            type: 'post',
            dataType: 'json',
            data: {'id':id},
            success: function (data) {
                if(data.data){
                    location.reload();
                }
            }
        })
    }
    //刷新
    refresh = function refresh(){
        location.reload();
    }
    //获取今日时间
    function getToday() {
        var myDate = new Date();
        var year = myDate.getFullYear();
        var month = myDate.getMonth() + 1;
        var date = myDate.getDate();
        var today = year + '-' + month + '-' + date;
        return today;
    }
    //返回按钮
    $('#goBack').click(function () {
        var title = $('#title').text();
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if(title == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });
});